package cn.skyquant.quant4j.api.economics;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.service.BaseServiceProxy;
import cn.skyquant.quant4j.sdk.enums.CompareType;
import cn.skyquant.quant4j.sdk.enums.TradeDirection;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class EconomicsServiceProxy extends BaseServiceProxy implements  EconomicsService{

    private static final String public_time_url = default_urlfix+"/economics/public_time";

    public EconomicsServiceProxy(String accountCode, BorkerType borkerType) {
        super(accountCode, borkerType);
    }


    @Override
    public ResultDTO<List<EconomicsEventDTO>> findEconomicsEventByDate(Date datetime) {
        return error;
    }

    @Override
    public ResultDTO<List<EconomicsDTO>> findEconomicsByDate(Date publicTime) {
        return error;
    }

    @Override
    public ResultDTO<List<EconomicsDTO>> findBetweenPublicTime(long from, long to) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("from",String.valueOf(from));
        paramMap.put("to",String.valueOf(to));
        JSONObject jsonObject = get(public_time_url,paramMap);
        if(jsonObject!=null){
            boolean ok = jsonObject.getBoolean("ok");
            String message = jsonObject.getString("message");
            if(ok){
                JSONArray data = jsonObject.getJSONArray("data");
                List<EconomicsDTO> list = convertJsonToEconomics(data);
                return ResultDTO.success(list);
            }else {
                return ResultDTO.error(message);
            }
        }
        return error;
    }

    private List<EconomicsDTO> convertJsonToEconomics(JSONArray jsonArray) {
        List<EconomicsDTO> list = new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++){
            EconomicsDTO dto = new EconomicsDTO();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            dto.id = jsonObject.getInteger("id");
            dto.yingxiang = jsonObject.getInteger("yingxiang");
            dto.actual = jsonObject.getDouble("actual");
            dto.consensus = jsonObject.getDouble("consensus");
            dto.unit = jsonObject.getString("unit");
            dto.revised = jsonObject.getString("revised");
            dto.publicTimestamp = jsonObject.getLong("publicTimestamp");
            dto.previous = jsonObject.getDouble("previous");
            dto.statusName = jsonObject.getString("statusName");
            dto.statusClass = jsonObject.getString("statusClass");
            dto.updatedTimestamp = jsonObject.getLong("updatedTimestamp");
            dto.updatedNum = jsonObject.getInteger("updatedNum");
            JSONArray impacts = jsonObject.getJSONArray("impacts");
            if(impacts!=null){
                for(int j=0;j<impacts.size();j++){
                    JSONObject impack = impacts.getJSONObject(j);
                    EconomicsCategoryImpactDTO impactDTO = new EconomicsCategoryImpactDTO();
                    impactDTO.categoryId = impack.getInteger("categoryId");
                    impactDTO.symbol = impack.getString("symbol");
                    impactDTO.type = CompareType.valueOf(impack.getString("type"));
                    impactDTO.td =  TradeDirection.parse(impack.getString("td"));
                    dto.impacts.add(impactDTO);
                }
            }
            list.add(dto);
        }
        return list;
    }
}
